home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c
- Subject: Re: simple code, argc, argv, strcmp()
- Date: Thu, 01 Feb 1996 12:50:27 GMT
- Organization: Netcom
- Message-ID: <3110b5eb.61577024@nntp.ix.netcom.com>
- References: <11f7cc$17261a.3b3@daprez>
- NNTP-Posting-Host: ix-dc9-28.ix.netcom.com
- X-NETCOM-Date: Thu Feb 01 4:50:50 AM PST 1996
- X-Newsreader: Forte Agent .99c/16.141
-
- otisg@panther.middlebury.edu (Otis Gospodnetic) wrote:
-
- > Hi, a C question...
- > this one has been bothering ALL afternoon !
- >
- > This snippet of code is supposed to call appropriate function based on what
- > command line arguments are supplied.
- >
- > usage: <program> -e [SourceFile] RemoteFile
- > or
- > <program> -d [InFile]
- >
- > if the person doesn't use this right the program is supposed to call Usage()
- > which is not here, but it's in my code, and if the person calls the program
- > the correct way one of the two actions should be taken (encode or decode).
- >
- > Problem - I am doing something wrong and I almost always end up calling
- > Usage() even if I use the program correctly.
- >
- > #include <stdio.h>
- > #include <stdlib.h>
- > #include <string.h>
- >
- > int main (int argc, char **argv) {
- >
- > void Usage (void);
- > void Encode (int, char **);
- > void Decode (int, char **);
- >
- > if (!strcmp(argv[1],"-d") || !strcmp(argv[1],"-e")) {
- > Usage();
- > }
- > if (strcmp(argv[1],"-d") && argc < 3) {
- > printf ("D %i\n", argc);
- > Usage();
- > }
- > if (strcmp(argv[1],"-e") && argc < 4) {
- > printf ("E %i\n", argc);
- > Usage();
- > }
- > if (strcmp(argv[1],"-d") && argc == 3) {
- > Decode(argc, argv);
- > }
- > if (strcmp(argv[1],"-e") && argc == 4) {
- > Encode(argc, argv);
- > }
- > return 0;
- > }
- >
- > Can someone see what's wrong with this ?
- > am I not using strcmp() right ?
- >
- > Thanks a bunch (I don't have any more hair to pull from my head !)
-
- You've got a few problems.
-
- When you think there's a problem with your use of a function, the
- first step should be to read the manual and find out what it really
- does. strcmp() returns 0 if the strings are equal, negative if the
- first compares less than the second, positive if the first compares
- greater. You seem to be assuming it will return nonzero if they are
- equal.
-
- Even when you correct that, your first test will be incorrect. Think
- about what it is doing -- you seem to be trying to test whether the
- first command line argument is either not equal to -d or not equal to
- -e. This will be true for all possible command line arguments.
-
- Finally, think about what happens if there are too many command line
- arguments. You try to call Usage() if there are too few and call the
- appropriate function if there are the right number, but simply return
- if there are too many.
-
-
- Michael M Rubenstein
-